package com.chao.yyks.util;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.chao.common.util.StrUtile;

import java.util.Collections;

/**
 * 通义千问请求接口
 */
public class QwenLong {
    public static String callWithMessage(String msg){
        try {
            Generation gen = new Generation();
            Message userMsg = Message.builder().role(Role.USER.getValue()).content(msg).build();
            GenerationParam param = GenerationParam.builder()
                .model("qwen-long")
                .messages(Collections.singletonList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .temperature(0.8f)
                .build();
            GenerationResult result =  gen.call(param);
            return result.getOutput().getChoices().get(0).getMessage().getContent();
        } catch (Exception e) {
            // 使用日志框架记录异常信息
            System.err.println("An error occurred while calling the generation service: " + e.getMessage());
        }
        return null;
    }


    public static void main(String[] args) {
        System.out.println(QwenLong.callWithMessage("这里有一篇参考范文：Vegetarianism, a dietary choice that excludes meat and often other animal products, offers a multitude of advantages for both individuals and the planet. One of the primary benefits is improved health. Studies have shown that vegetarians have a lower risk of developing chronic diseases such as heart disease, hypertension, and certain types of cancer. This is largely due to the high intake of fruits, vegetables, and whole grains, which are rich in essential nutrients, antioxidants, and fiber. These components support overall well-being and can help manage weight, leading to a lower body mass index.\n" +
                "\n" +
                "From an environmental perspective, vegetarianism is more sustainable. Meat production requires significant amounts of land, water, and energy, and it contributes substantially to greenhouse gas emissions and deforestation. By choosing a plant-based diet, individuals can reduce their carbon footprint and contribute to a healthier planet. Additionally, vegetarianism aligns with ethical considerations regarding animal welfare, promoting principles of non-violence and compassion.\n" +
                "\n" +
                "Economically, vegetarianism can be more affordable. Plant-based foods are generally cheaper than meat, making healthy eating more accessible to a broader population. This can help address issues of food insecurity and promote better nutrition in communities where resources are limited.\n" +
                "\n" +
                "In summary, adopting a vegetarian lifestyle not only enhances personal health but also supports environmental sustainability, ethical values, and economic accessibility, making it a beneficial choice for many." +
                "按照下列维度对作业评分并举例说明需要改进的地方:\n1.内容：作文内容与范文主题内容一致，围绕主题进行了充分的阐述。2分\n" +
                "2.衔接与连贯：行文连贯，段落之间过渡自然，句子之间衔接紧密，恰当使用了衔接性词语，表达因果关系，转折关系，递进关系等逻辑关系。2分\n" +
                "3.词汇：准确恰当地使用词汇。既包含频率高的词，也包含使用频率较低的复杂词汇，表达抽象意义。2分\n" +
                "4.句法：灵活准确地使用不同的语法结构。包括简单句型，也包括复杂的句型。语法错误少。2.5分\n作业:" +
                "In recent years, the trend of vegetarianism has swept the world from Western societies. And Vegetarianism has also .some of them eat vegetarian food and eggs. Whether it's a desire to help the environment, religious beliefs or weight loss, more and more people are opting for vegetarianism. However, people have mixed opinions about vegetarianism.\n" +
                "As for me, I agree with vegetarianism.shown that vegetarianism can help reduce the risk of diabetes and improve heart and lung function. Secondly, vegetarianism can improve the earth's environment. Vegetarianism can reduce environmental pollution from animal husbandry and is conducive to sustainable development. What's more,A vegetarian diet is also a good option for those who are trying to lose weight. Vegetarianism can help people clean out their intestines and thus lose weight.\n" +
                "" +
                "返回格式为纯json对象字符串,不要包含数组,饱含a1(内容评价),a2(衔接与连贯评价),a3(词汇评价),a4(句法评价),a5(内容得分),a6(衔接与连贯得分),a7(词汇得分),a8(句法得分),corrections(润色修正),score(总得分)字段"));
    }
}